package com.example.gatewayserver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.security.Principal;

@RestController
public class ServiceInstallController {

    @LoadBalanced
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/demo1")
    @PreAuthorize("hasAnyRole('USER')")
    public String test() {
        return "a: " + restTemplate.getForObject("http://DEMO1/test", String.class);
    }


    @RequestMapping(path = "/redis")
    @PreAuthorize("hasAnyRole('USER')")
    public String redisTest(Principal principal) {
        return principal.getName();
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
